package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.GoodsBrandService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*

@RequestMapping("goodsBrands")
@RestController
class GoodsBrandController(
    private val goodsBrandService: GoodsBrandService
) {

    data class QueryGoodsBrandsRequest(
        val name: String?,
        val groupIds:List<Int> = emptyList(),
        val disable: Boolean?,
    )

    @HasAuthority(Authority.货品_品牌_查询)
    @GetMapping(params = ["page"])
    fun queryGoodsBrands(pageable: Pageable, queryGoodsBrandsRequest: QueryGoodsBrandsRequest): Page<QueryGoodsBrandsResponse> {
        return this.goodsBrandService.queryGoodsBrands(pageable,queryGoodsBrandsRequest)
    }

    data class QueryGoodsBrandsResponse(
        val name:String,
        val id:Int,
        val goodsAmount:Int,
        val group:IntIdAndName?,
        val factory:IntIdAndName?,
        val tags: List<IntIdAndName>,
        val disable: Boolean,
    )

    data class UpdateGoodsBrandRequest(
        val name: String,
        val groupId:Int?,
        val factoryId:Int?,
        val tagIds: List<Int> = emptyList(),
    )

    @HasAuthority(Authority.货品_品牌_修改)
    @PutMapping("{id}")
    fun updateGoodsBrand(@PathVariable id:Int,@RequestBody updateGoodsBrandRequest: UpdateGoodsBrandRequest){
        this.goodsBrandService.updateGoodsBrand(id,updateGoodsBrandRequest)
    }

    @HasAuthority(Authority.货品_品牌_新增)
    @PostMapping
    fun createGoodsBrand(@RequestBody updateGoodsBrandRequest: UpdateGoodsBrandRequest){
        this.goodsBrandService.createGoodsBrand(updateGoodsBrandRequest)
    }

    @DeleteMapping("{id}")
    fun deleteGoodsBrand(@PathVariable id:Int){
        this.goodsBrandService.deleteGoodsBrand(id)
    }

    @GetMapping
    fun getAllGoodsBrands():List<GoodsBrandSummary>{
        return this.goodsBrandService.getAllGoodsBrands()
    }

    data class GoodsBrandSummary(
        val id:Int,
        val name: String,
    )

    @PatchMapping("{id}/disable")
    fun disable(@PathVariable id:Int){
        this.goodsBrandService.disable(id)
    }

    @PatchMapping("{id}/enable")
    fun enable(@PathVariable id:Int){
        this.goodsBrandService.enable(id)
    }

}